package com.stlm2.util;

import com.sun.imageio.plugins.common.ImageUtil;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.Map;


public class WatermarkUtils {

    /**
     * 把二维码印刷到图片上
     *
     * @param sourceImg 目标文件
     * @param qrcode    二维码
     */
    public final static void pressQrcode(String sourceImg, String qrcode, int qrcodeWidth, int qrcodeHeight, String targetImg) {
        pressQrcode(sourceImg, qrcode, qrcodeWidth, qrcodeHeight, targetImg, null, null);
    }

    /**
     * 把二维码印刷到图片上
     *
     * @param sourceImg 水印文件
     * @param qrcode    二维码
     * @param x         x坐标
     * @param y         y坐标
     */
    public final static void pressQrcode(String sourceImg, String qrcode, int qrcodeWidth, int qrcodeHeight, String targetImg, Integer x, Integer y) {
        try {
            //源文件
            File src_file = new File(sourceImg);
            Image src_image = ImageIO.read(src_file);

            //二维码文件
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            QrcodeUtils.encode(qrcode, qrcodeWidth + 100, qrcodeHeight + 100, stream);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());

            BufferedImage press_image = ImageIO.read(inputStream);
            press_image = Thumbnails.of(press_image).size(qrcodeHeight, qrcodeHeight).asBufferedImage();

            //输出流
            FileOutputStream out = new FileOutputStream(targetImg);

            press(src_image, press_image, out, x, y);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把二维码印刷到图片上
     *
     * @param out       响应
     * @param sourceImg 目标文件
     * @param qrcode    二维码
     */
    public final static void pressQrcode(String sourceImg, String qrcode, int qrcodeWidth, int qrcodeHeight, OutputStream out) {
        pressQrcode(sourceImg, qrcode, qrcodeWidth, qrcodeHeight, out, null, null);
    }

    /**
     * 把二维码印刷到图片上
     *
     * @param out       响应
     * @param sourceImg 水印文件
     * @param qrcode    二维码
     * @param x         x坐标
     * @param y         y坐标
     */
    public final static void pressQrcode(String sourceImg, String qrcode, int qrcodeWidth, int qrcodeHeight, OutputStream out, Integer x, Integer y) {
       /* String path1=path+sourceImg;*/
        try {
            //源文件
            File src_file = new File(sourceImg);
            Image src_image = ImageIO.read(src_file);

            //二维码文件
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            QrcodeUtils.encode(qrcode, qrcodeWidth + 100, qrcodeHeight + 100, stream);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());

            BufferedImage press_image = ImageIO.read(inputStream);
            press_image = Thumbnails.of(press_image).size(qrcodeHeight, qrcodeHeight).asBufferedImage();

            press(src_image, press_image, out, x, y);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把二维码印刷到图片上
     *
     * @param out       响应
     * @param x         x坐标
     * @param y         y坐标
     */
    public final static void pressQrcode(Image image, String id, int qrcodeWidth, int qrcodeHeight, OutputStream out, Integer x, Integer y) {
       /* String path1=path+sourceImg;*/
        try {
            //二维码文件
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            QrcodeUtils.encode(id, qrcodeWidth + 100, qrcodeHeight + 100, stream);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());

            BufferedImage press_image = ImageIO.read(inputStream);
            press_image = Thumbnails.of(press_image).size(qrcodeHeight, qrcodeHeight).asBufferedImage();

            press(image, press_image, out, x, y);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把图片印刷到图片上
     *
     * @param sourceImg 目标文件
     * @param pressImg  水印文件
     */
    public final static void pressImage(String sourceImg, String pressImg, String targetImg) {
        pressImage(sourceImg, pressImg, targetImg, null, null);
    }

    /**
     * 把图片印刷到图片上
     *
     * @param sourceImg 水印文件
     * @param pressImg  目标文件
     * @param targetImg 输出位置
     * @param x         x坐标
     * @param y         y坐标
     */
    public final static void pressImage(String sourceImg, String pressImg, String targetImg, Integer x, Integer y) {
        try {
            //源文件
            File src_file = new File(sourceImg);
            Image src_image = ImageIO.read(src_file);

            //水印文件
            File press_file = new File(pressImg);
            Image press_image = ImageIO.read(press_file);

            //输出流
            FileOutputStream out = new FileOutputStream(targetImg);

            press(src_image, press_image, out, x, y);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把图片印刷到图片上
     *
     * @param sourceImg 目标文件
     * @param pressImg  水印文件
     */
    public final static void pressImage(String sourceImg, String pressImg, OutputStream out) {
        pressImage(sourceImg, pressImg, out, null, null);
    }

    /**
     * 把图片印刷到图片上
     *
     * @param sourceImg 水印文件
     * @param pressImg  目标文件
     * @param x         x坐标
     * @param y         y坐标
     */
    public final static void pressImage(String sourceImg, String pressImg, OutputStream out, Integer x, Integer y) {
        try {
            //源文件
            File src_file = new File(sourceImg);
            Image src_image = ImageIO.read(src_file);

            //水印文件
            File press_file = new File(pressImg);
            Image press_image = ImageIO.read(press_file);

            press(src_image, press_image, out, x, y);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片叠加并输出到流
     *
     * @param src_image   水印文件
     * @param press_image 二维码
     * @param out         输出流
     * @param x           x坐标
     * @param y           y坐标
     */
    private final static void press(Image src_image, Image press_image, OutputStream out, Integer x, Integer y) {
        try {
            //源文件
            int src_width = src_image.getWidth(null);
            int src_height = src_image.getHeight(null);
            BufferedImage image = new BufferedImage(src_width, src_height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(src_image, 0, 0, src_width, src_height, null);

            //二维码文件
            int press_width = press_image.getWidth(null);
            int press_height = press_image.getHeight(null);
            if (x == null || y == null) {//如果没有指定图片位置，则默认中间位置
                x = (src_width - press_width) / 2;
                y = (src_height - press_height) / 2;
            }
            g.drawImage(press_image, x, y, press_width, press_height, null);

            //生成文件
            g.dispose();
            ImageIO.write(image, "JPG", out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把分销商信息打印到授权证书上
     *
     * @param out        响应
     * @param sourceFile 水印文件
     * @param pressWords 印刷的文字信息
     * @param out        输出来
     */
    public final static void pressLicense(String sourceFile, Map<String, int[]> pressWords, OutputStream out) {
        try {
            Color color = Color.black;  //字体颜色
            ImageIcon imgIcon = new ImageIcon(sourceFile);
            Image theImg = imgIcon.getImage();
            //Image可以获得 输入图片的信息
            int width = theImg.getWidth(null);
            int height = theImg.getHeight(null);

            //画出图片的大小
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            //2d 画笔
            Graphics2D g = image.createGraphics();
            g.setColor(color);
            g.setBackground(Color.white);
            //g.getFont().getFontName();//获取当前字体  暂不使用   2016年9月22日 16:18:14  付春雨
            //画出图片-----------------------------------
            g.drawImage(theImg, 0, 0, null);
            for (String key : pressWords.keySet()) {
                int[] infoArr = pressWords.get(key);
                AttributedString atsName = new AttributedString(key);
                Font wordFont = new Font("宋体", Font.BOLD, infoArr[0]);
                atsName.addAttribute(TextAttribute.FONT, wordFont, 0, key.length());
                AttributedCharacterIterator iter = atsName.getIterator();
                g.drawString(iter, infoArr[1], infoArr[2]);

            }

            g.dispose();//画笔结束

            ImageIO.write(image, "JPG", out);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把分销商信息打印到授权证书上
     *
     * @param sourceFile 水印文件
     * @param pressWords 印刷的文字信息
     */
    public final static Image pressLicense(String sourceFile, Map<String, int[]> pressWords) {
        try {
            Color color = new Color(252,229,103);  //字体颜色

            URL url=new URL(sourceFile);
            Image theImg = ImageIO.read(url);
            //Image可以获得 输入图片的信息
            int width = theImg.getWidth(null);
            int height = theImg.getHeight(null);

            //画出图片的大小
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            //2d 画笔
            Graphics2D g = image.createGraphics();
            g.setColor(color);
            g.setBackground(Color.white);
            //g.getFont().getFontName();//获取当前字体  暂不使用   2016年9月22日 16:18:14  付春雨
            //画出图片-----------------------------------
            g.drawImage(theImg, 0, 0, null);
            for (String key : pressWords.keySet()) {
                int[] infoArr = pressWords.get(key);
                AttributedString atsName = new AttributedString(key);
                Font wordFont = new Font("黑体", Font.BOLD, infoArr[0]);
                atsName.addAttribute(TextAttribute.FONT, wordFont, 0, key.length());
                AttributedCharacterIterator iter = atsName.getIterator();
                g.drawString(iter, infoArr[1], infoArr[2]);

            }

            g.dispose();//画笔结束
            return (Image) image;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
//        pressQrcode("g:/picture/test_1.jpg", "http://5.dshui.cc", 200, 200, "g:/picture/test_2.jpg", 300, 625);
//        pressQrcode("f:/1.jpg", "http://6.dshui.cc", 200, 200, "f:/new6.jpg");
//        createMark("g:/picture/honor.jpg", "g:/picture/honor_1.jpg", "", null, 1,"",100);
    }
}
